<?php


namespace App\Models;


use App\Traits\DateTimeModel;
use App\Traits\Model\OrderByIdDesc;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Receiving extends Model
{
    use DateTimeModel, OrderByIdDesc, SoftDeletes;

    protected $table = 'receiving';
    protected $fillable = [
        'customer_id', 'district', 'type', 'weight', 'price', 'transport', 'packing', 'piece', 'code', 'remark', 'product_count'
    ];

    protected static function boot()
    {
        static::addGlobalScope('orderBy', function (Builder $query) {
            $query->orderByDesc('id');
        });
        parent::boot();
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     * @author luffyzhao@vip.126.com
     */
    public function products()
    {
        return $this->hasMany(ReceivingProduct::class);
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     * @author luffyzhao@vip.126.com
     */
    public function logs()
    {
        return $this->hasMany(ReceivingLog::class);
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     * @author luffyzhao@vip.126.com
     */
    public function files()
    {
        return $this->hasMany(ReceivingFile::class);
    }

    /**
     * 可以修改
     * @return bool
     */
    public function canModify(){
        return $this->isDraft() || $this->isReject();
    }

    /**
     * 可以删除
     * @return bool
     */
    public function canDelete(){
        return $this->isDraft() || $this->isReject();
    }
    /**
     * 草稿状态
     * @return bool
     * @author luffyzhao@vip.126.com
     */
    public function isDraft(){
        return $this->getAttribute('status') === '0';
    }

    /**
     * 驳回
     * @return bool
     */
    public function isReject(){
        return $this->getAttribute('status') === '2';
    }
}
